/**
 * Copyright (C) 2011 Audit Logger Project. All rights reserved.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.auditlogger.api;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.auditlogger.config.ConfigurationServices;
import org.auditlogger.config.util.ConfigurationException;
import org.auditlogger.config.util.Constants;

/**
 * Provide clients with an <code>AuditLogger</code>.
 * 
 * @author <a href="mailto:ravi.sharda@gmail.com">Ravi Sharda</a>
 */
public abstract class AuditLoggerFactory {
	
	private static volatile AuditLoggerFactory auditLogFactory = null;
	
	private static final Log logger = LogFactory.getLog(AuditLoggerFactory.class);
	
	/**
	 * Gets the <Code>AuditLogger</code> associated with the specified module name. 
	 * The module is used by the logger to log which module is generating 
	 * the log events. A module name is an arbitrary name. 
	 * <p>
	 * Sample options for module names include:
	 * <ol> 
	 *     <li>a) just the class name, such as MyClass</li> 
	 *     <li>a fully qualified class name, com.myco.MyClass</li>
	 *     <li> a subsystem name and class name combination, such as AUTHORIZATION.MyClass, or 
	 *     simply AUTHORIZATION, etc. </li> 
	 * </ol>
	 * @param moduleName The name of the module requesting the logger.
	 * @return The Logger associated with this module.
	 */
	public abstract AuditLogger getLogger(String moduleName);
	
	/**
	 * Gets the logger associated with the specified class. The class is 
	 * used by the logger to log which class is generating the log events. 
	 * 
	 * @param clazz
	 * 			The name of the class requesting the logger.
	 * @return
	 * 			The Logger associated with this class.
	 */
	@SuppressWarnings("rawtypes")
	public abstract AuditLogger getLogger(Class clazz);
	
	/**
	 * The "service" method to get a pre-configured Audit Log Factory such as 
	 *  Log4jLogFactory, etc.
	 */
	public static AuditLoggerFactory getInstance() throws ConfigurationException {
		AuditLoggerFactory result = 
			ConfigurationServices.getPluginConfigurator().getPlugin(
					Constants.LOG_FACTORY_PROVIDER);
		logger.info("instantiated log factory: " + result);
		return result;
	}

}